package com.loukou.lkt.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loukou.lkt.lkt.bo.AllocationOrderBo;
import com.loukou.lkt.lkt.bo.PageList;
import com.loukou.lkt.lkt.bo.PoPurchaseReturnOrderFinaceBo;
import com.loukou.lkt.lkt.bo.PoPurchaseReturnOrderFinaceListBo;
import com.loukou.lkt.lkt.enums.PoPurchaseReturnOrderStateEnum;
import com.loukou.lkt.lkt.req.PurchaseOrderReqDto;
import com.loukou.lkt.lkt.req.PurchaseOrderReturnReqDto;
import com.loukou.lkt.lkt.resp.AuthInfo;
import com.loukou.lkt.lkt.resp.PageModel;
import com.loukou.lkt.lkt.resp.PoPurchaseOrderReturnDetailRespDto;
import com.loukou.lkt.lkt.resp.PoPurchaseReturnOrderGoodsDto;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.resp.WarehouseDto;
import com.loukou.lkt.lkt.service.WarehouseService;
import com.loukou.lkt.web.processor.PoPurchaseReturnProcessor;
import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("/purchaseReturnOrder")
public class PoPurchaseReturnController extends BaseController{	
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private PoPurchaseReturnProcessor poReturnProcessor;
	
	@Autowired
	WarehouseService warehouseService;
	
	@RequestMapping("purchaseReturnOrderDetail")
	public String purchaseOrderDetail(@RequestParam int supplierId,@RequestParam String supplierName, Model model){
		List<WarehouseDto> list = warehouseService.getAllWarehouseInfo();
		model.addAttribute("supplierName", supplierName);
		model.addAttribute("supplierId", supplierId);
		model.addAttribute("warehouses", list);
		return "purchase/purchaseReturnOrderDetail";
	}
	
	@RequestMapping(value = "/finaceCheckDetail", method = RequestMethod.GET)
	@ResponseBody
	public PoPurchaseReturnOrderFinaceBo view(
			@RequestParam(value = "orderId", required = false, defaultValue = "0") int orderId){
		PoPurchaseReturnOrderFinaceBo bo=poReturnProcessor.getReturnOrderByOrderId(orderId);
		return bo;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(int orderId){
		ModelAndView mv = new ModelAndView("purchaseReturnOrder/finaceCheckDetail");
		return mv;
	}
	
	@RequestMapping("/index")
	public String index(){
		return "purchaseReturnOrder/financeCheck";
	}	
	
	@RequestMapping(value = "/finaceCheckList", method = RequestMethod.GET)
	@ResponseBody
	public RespDto<PageList<PoPurchaseReturnOrderFinaceListBo>> getListData(
			@RequestParam(value = "orderState", required = false, defaultValue = "-1") int orderState,
			@RequestParam(value = "returnOrder", required = false, defaultValue = "") String returnOrder,
			@RequestParam(value = "supplierCode", required = false, defaultValue = "") String supplierCode,
			@RequestParam(value = "supplierName", required = false, defaultValue = "") String supplierName,
			@RequestParam(value = "purchaseType", required = false, defaultValue = "1") int purchaseType,
			@RequestParam(value = "returnPerson", required = false, defaultValue = "") String returnPerson,
			@RequestParam(value = "startReturnTime", required = false, defaultValue = "") String startReturnTime,
			@RequestParam(value = "endReturnTime", required = false, defaultValue = "") String endReturnTime,
			@RequestParam(value = "pageIndex", required = false, defaultValue = "1") int pageIndex,
			@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize){
		
		Date startTime = null;
		Date endTime = null;
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{if(!StringUtils.isNullOrEmpty(startReturnTime)){
			startTime = sf.parse(startReturnTime+" 00:00:00");}
	    }catch(Exception e){e.printStackTrace();}
		try{if(!StringUtils.isNullOrEmpty(endReturnTime)){
			endTime = sf.parse(endReturnTime+" 23:59:59");}
	    }catch(Exception e){e.printStackTrace();}
						
		RespDto<PageList<PoPurchaseReturnOrderFinaceListBo>> res=new RespDto<PageList<PoPurchaseReturnOrderFinaceListBo>>();
		res=poReturnProcessor.getFinaceCheckReturnOrderList(orderState,
				returnOrder, supplierCode, supplierName, purchaseType, returnPerson,
				startTime, endTime, pageIndex, pageSize);
		return res;
	}
	
	@RequestMapping(value = "/finaceAudit", method = RequestMethod.GET)
	@ResponseBody
	public RespDto<String> finaceAuditReturnOrder(
			@RequestParam(value = "orderReturnId", required = false, defaultValue = "0") int orderReturnId,
			@RequestParam(value = "orderReturnCode", required = false, defaultValue = "") String orderReturnCode){
		RespDto<String> res=new RespDto<String>();		
		HttpSession session = request.getSession();
		AuthInfo info = (AuthInfo) session.getAttribute("authInfo");
		res=poReturnProcessor.finaceAuditReturnOrder(orderReturnId, info,orderReturnCode);
		return res;
	}
	
	@RequestMapping(value = "/finaceReject", method = RequestMethod.GET)
	@ResponseBody
	public RespDto<String> finaceRejectReturnOrder(
			@RequestParam(value = "orderReturnId", required = false, defaultValue = "0") int orderReturnId,
			@RequestParam(value = "orderReturnCode", required = false, defaultValue = "") String orderReturnCode){
		RespDto<String> res=new RespDto<String>();
		HttpSession session = request.getSession();
		AuthInfo info = (AuthInfo) session.getAttribute("authInfo");
		res=poReturnProcessor.finaceRejectReturnOrder(orderReturnId,info,orderReturnCode);
		return res;
	}
	
	@RequestMapping(value = "getWarehouseGoods", method = RequestMethod.GET)
	@ResponseBody
	public List<PoPurchaseReturnOrderGoodsDto> getWarehouseGoodsBySupplierId(
			@RequestParam(value = "warehouseId") int warehouseId,@RequestParam(value = "supplierId") int supplierId,
			@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = false, defaultValue = "1000") int pageSize){
		List<PoPurchaseReturnOrderGoodsDto> res = poReturnProcessor.getReturnOrderInit(warehouseId, supplierId).getReturnOrderGoods();
		return res;
	}
	
	@RequestMapping(value="submitReturnOrder",consumes="application/json")
	@ResponseBody
	public RespDto<String> submitReturnOrder(PurchaseOrderReturnReqDto req){
		//TODO
		RespDto<String> result = poReturnProcessor.submitReturnOrder(req);
//		RespDto<String> result = new RespDto<String>();
        return result;
	}
}
